package com.ohimy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.ohimy.dao.impl.GroupDao;
import com.ohimy.dao.impl.GroupPhotoDao;
import com.ohimy.entity.Group;
import com.ohimy.entity.GroupPhoto;

@Service("photoService")
public class PhotoService {

	@Resource(name = "groupPhotoDao")
	private GroupPhotoDao groupPhotoDao;

	@Resource(name = "groupDao")
	private GroupDao groupDao;

	/**
	 * 得到所有该群组的图片总数
	 * 
	 * @param id
	 * @return
	 */
	public List<GroupPhoto> getPhotoAllInTen(long id) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				groupDao.get(id)) };
		return groupPhotoDao
				.findByCriteria(0, 10, Order.desc("id"), criterions);
	}

	/**
	 * 得到所有该群组的图片总数
	 * 
	 * @param id
	 * @return
	 */
	public long getPhotoByTotal(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(GroupPhoto.class);
		criteria.add(Restrictions.eq("group", getGroupById(id)));
		return groupPhotoDao.findByCriteria(criteria).size();
	}

	/**
	 * 相册的分页
	 * 
	 * @param id
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public List<GroupPhoto> getPhotoByPager(long gid, int firstResult,
			int maxResult) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				getGroupById(gid)) };
		return groupPhotoDao.findByCriteria(firstResult, maxResult, Order
				.desc("id"), criterions);
	}

	/**
	 * 保存相片
	 * 
	 * @param groupPhoto
	 * @return
	 */
	public long save(GroupPhoto groupPhoto) {
		return groupPhotoDao.save(groupPhoto);
	}

	/**
	 * 通过ID返回群组对象
	 * 
	 * @param id
	 * @return
	 */
	public Group getGroupById(long id) {
		return groupDao.get(id);
	}

	/* ********************************* */
	public void setGroupPhotoDao(GroupPhotoDao groupPhotoDao) {
		this.groupPhotoDao = groupPhotoDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

}
